package com.atguigu.guli.edu.controller.admin;

import com.atguigu.common_utils.utils.PageUtils;
import com.atguigu.guli.edu.entity.Course;
import com.atguigu.guli.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.edu.entity.query.CourseInfoSearch;
import com.atguigu.guli.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.edu.service.EduCourseService;
import com.atguigu.guli.service.base.result.R;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 课程
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-19 10:59:18
 */
@RestController

@Api(tags = "课程信息")
@RequestMapping("/admin/edu/course")
public class AdminEduCourseController {
    @Autowired
    private EduCourseService eduCourseService;


    @ApiOperation("保存课程信息")
    @PostMapping("/save")
    public R saveInfo(@RequestBody CourseInfoForm courseInfoForm){
        String id = eduCourseService.saveInfo(courseInfoForm);
        return R.ok().data("data",id);
    }

    @ApiOperation("修改课程信息")
    @PutMapping("/update/{id}")
    public R uodateInfo(@RequestBody CourseInfoForm courseInfoForm,@PathVariable("id") String courseId){
        eduCourseService.updateInfo(courseInfoForm,courseId);
        return R.ok();
    }

    @ApiOperation("根据id查询课程信息")
    @GetMapping("/select/{id}")
    public R getById(@PathVariable("id")String id){
        CourseInfoForm courseInfoForm = eduCourseService.selectById(id);
        return R.ok().data("data",courseInfoForm);
    }

    @ApiOperation("分页查询课程信息")
    @GetMapping("/List/{currPage}/{pagesize}")
    public R listCourse(@PathVariable("currPage")Integer currPage,
                        @PathVariable("pagesize")Integer pagesize,
                        CourseInfoSearch courseInfoSearch){
        Page<CourseInfoVo> courseInfoVos = eduCourseService.pageList(currPage,pagesize,courseInfoSearch);
        return R.ok().data("data",courseInfoVos);
    }

    @ApiOperation("根据id查询课程详细信息")
    @GetMapping("/courseInfo/{id}")
    public R getByIdCourseInfo(@PathVariable("id")String id){
        CourseInfoVo courseInfoVo = eduCourseService.getByIdCourseInfo(id);
        return R.ok().data("data",courseInfoVo);
    }

    @ApiOperation("发布课程")
    @GetMapping("/publish/{courseId}")
    public R publish(@PathVariable String courseId){
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",courseId);
        updateWrapper.set("status","Normal");
        updateWrapper.set("publish_time",new Date());
        eduCourseService.update(updateWrapper);
        return R.ok();
    }

}
